;
(function($) {
    $.tools= {
        getPos: function(options) {
            options=$.extend({
                ele:null
            },options|| {});
            var $_obj=$(options.ele);
            var _elePos= {};
            $_obj.each( function() {
                _elePos._l=$(this).offset().left;
                _elePos._t=$(this).offset().top;
                _elePos._w=$(this).outerWidth();
                _elePos._h=$(this).outerHeight();
            });
            return _elePos;
        },
        stringToDateTime: function(postdate) {
            var second = 1000;
            var minutes = second * 60;
            var hours = minutes * 60;
            var days = hours * 24;
            var months = days * 30;
            var twomonths = days * 365;
            var myDate = new Date(Date.parse(postdate));
            if (isNaN(myDate)) {
                myDate = new Date(postdate.replace(/-/g, "/"));
            }
            var nowtime = new Date();
            var longtime = nowtime.getTime() - myDate.getTime();
            var showtime = 0;
            if (longtime > months * 12) {
                return Math.floor(longtime/(months * 12)) +"年前";
            } else if (longtime > months) {
                return Math.floor(longtime/months) +"个月前";
            } else if (longtime > days * 7) {
                return ("1周前");
            } else if (longtime > days) {
                return (Math.floor(longtime / days) + "天前");
            } else if (longtime > hours) {
                return (Math.floor(longtime / hours) + "小时前");
            } else if (longtime > minutes) {
                return (Math.floor(longtime / minutes) + "分钟前");
            } else if (longtime > second) {
                return (Math.floor(longtime / second) + "秒前");
            } else {
                return "刚刚";
            }
        }
    };
    $.extend(String.prototype, {
        is_email: function() {
            var s = $.trim(this);
            return /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/.test(s);
        },
        is_password: function() {
            return /^.{6,32}$/.test(this);
        },
        validate_name: function() {
            var name = this;
            if (name.length > 32) {
                return "too_long";
            } else if (/^.*[!-'*-,\/:-@[-`{-~0-9\u00B7\uFF01\uFFE5\u2026\u2014\u3010\u3011\uFF1B\uFF1A\u2018\u201C\uFF0C\u300A\u3002\u300B\u3001\uFF1F].*$/.test(name)) { // special symbol and numerical character
                return "invalid_symbol";
            } else if (/^\s*[a-zA-Z\s.\-]{2,}\s*$/.test(name)) { // pure English
                if (/^[a-zA-Z]+((\s|\s?\.\s?|\s?-\s?)[a-zA-Z]+){0,}(\s|\s?\.\s?|\s?-\s?)[a-zA-Z]+$/.test(name)) { // doesn't conform to English name format
                    return "success_validation";
                } else {
                    return "invalid_english";
                }
            } else if (/^\s*[\u4e00-\u9fa5\s]{1,}\s*$/.test(name)) { // pure Chinese
                if (/^[\u4e00-\u9fa5]{2,10}$/.test(name)) { // doesn't conform to Chinese name format
                    return "server_validation";
                } else {
                    return "invalid_chinese";
                }
            } else if (/^.*[\u4e00-\u9fa5].*[a-zA-Z].*$/.test(name)) { // Chinese(English)
                if (/^[\u4e00-\u9fa5]{2,10}\s*[\(\uFF08]?[a-zA-Z]+((\s|\s?\.\s?|\s?-\s?)[a-zA-Z]+){0,4}((\s|\s?\.\s?|\s?-\s?)[a-zA-Z]+)?[\)\uFF09]?$/.test(name)) { // doesn't conform to format
                    return "server_validation"
                } else {
                    return "invalid_chinese_english";
                }
            } else if (/^.*[a-zA-Z].*[\u4e00-\u9fa5].*$/.test(name)) { // English(Chinese)
                if (/^[a-zA-Z]+((\s|\s?\.\s?|\s?-\s?)[a-zA-Z]+){0,4}((\s|\s?\.\s?|\s?-\s?)[a-zA-Z]+)?\s*[[\(\uFF08]?[\u4e00-\u9fa5]{2,10}[\)\uFF09]?$/.test(name)) {
                    return "server_validation";
                } else {
                    return "invalid_english_chinese";
                }
            } else if (/^.*[\u3040-\u30FF\u31F0-\u31FF\uAC00-\uD7AF\u1100-\u11FF\u00C0-\u024F]+.*$/.test(name)) { // other language
                return "success_validation";
            } else {
                return "invalid_name";
            }
        },
        is_name: function() {
            //return /[\u4e00-\u9fa5\w\/\(\)\s]{2,}/.test(this) &&  !( this.indexOf("\'") != -1 || this.indexOf('>') != -1 || this.indexOf('<') != -1 || $.trim(this).length <=0 || this.length > 25);
            //return /(^\s*[\u4e00-\u9fa5]{2,4}\s*$)|(^\s*([a-zA-Z]+(\.{1}[a-zA-Z]+)?){2,32}\s*$)|(^\s*([a-zA-Z]+(\s{1}[a-zA-Z]+)?){2,32}\s*$)/.test(this);
            return /^.{2,25}$/.test(this) && (!/[^\u4e00-\u9fa5\w\/\(\)\s]/.test(this));
        },
        is_illegalcontent: function() {
            return (/[^\w]/.test(this));
        },
        is_url: function() {
            //var re = new RegExp('^http[s]?:\/\/(\\w+\\.\\w+\/?)([\\w.]+\/?)*(\\?.[^\\?\\s]*)?$', 'ig');
            var re = new RegExp('^http[s]?:\/\/([\\w.]+\/?)\\S*$', 'ig');
            return re.test(this);
        },
        is_qq: function() {
            return /^[\d]{5,10}$/.test(this);
        },
        is_mobile: function() {
            return /^[\d\s]{5,20}$/.test(this);
        },
        trim: function() {
            return this.replace(/\s{2,}/g, ' ');
        },
        toText: function() {
            return this.replace(/<\/?[^>]+>/g, '');
        },
        nlTobr: function() {
            return this.replace(/\r\n|\r|\n/g, '<br />');
        },
        brTonl: function() {
            return this.replace(/<br[\s]*[\/]?>/ig, '\r\n');
        },
        htmlTostr: function() {
            var str = this.replace(/</g, '&lt;');
            str = str.replace(/>/g, '&gt;');
            //str = str.replace(/ /g, '&nbsp;');
            return str.nlTobr();
        },
        strTohtml: function() {
            var str = this.brTonl();
            str = str.replace(/&lt;/g, '<');
            str = str.replace(/&gt;/g, '>');
            return str.replace(/&nbsp;/g, ' ');
        },
        is_correct_suffix : function(suffix) {
            suffix = suffix.toLowerCase();
            var temp_suffix = this.substring(this.lastIndexOf('.')+1).toLowerCase();
            return (suffix.indexOf(temp_suffix)>=0);
        },
        //判断是一个正整数或0
        isNumber: function() {
            return /^[0-9]*$/.test(this);
        },
        //判断是否为大写字母
        isUpperLetter: function() {
            return /^[A-Z]+$/.test(this);
        },
        validateLength : function(length) {
            return this.length<=length;
        },
        getStringLength: function() {
            var len = 0;
            if (this.length <= 0) {
                len = 0;
            } else {
                for (var i=0;i<this.length;i++) {
                    if (this.charCodeAt(i) < 256) {
                        len = len + 1;
                    } else {
                        len = len + 2;
                    }
                }
            }
            return len;
        }
    });
    $.extend(Date.prototype, {
        /**
         * 时间对象的格式化:"yyyy-MM-dd hh:mm:ss"
         */
        format: function(format) {
            var o = {
                "M+" : this.getMonth() + 1, // month
                "d+" : this.getDate(), // day
                "h+" : this.getHours(), // hour
                "m+" : this.getMinutes(), // minute
                "s+" : this.getSeconds(), // second
                "q+" : Math.floor((this.getMonth() + 3) / 3), // quarter
                "S" : this.getMilliseconds()// millisecond
            }
            if (/(y+)/.test(format)) {
                format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
            }
            for (var k in o) {
                if (new RegExp("(" + k + ")").test(format)) {
                    format = format.replace(RegExp.$1, RegExp.$1.length == 1? o[k]: ("00" + o[k]).substr(("" + o[k]).length));
                }
            }
            return format;
        }
    });
    if(typeof(LazyLoad)!='undefined') {
        LazyLoad.css('http://static.aceona.com/app_js/components/tipError/tipError.css');
        LazyLoad.js('http://static.aceona.com/app_js/components/tipError/tipError.js');
    }
})(jQuery);